import type { TableColumn } from "@/components/DynamicTable/types";
import { h } from "vue";
import { router } from "@/router";

// 漏洞等级映射
export const vulnLevelMap = {
  高危: { text: "高危", type: "danger" },
  中危: { text: "中危", type: "warning" },
  低危: { text: "低危", type: "info" },
};

// 表格列配置工厂函数
export const createTableColumns = (): TableColumn[] => [
  {
    label: "序号",
    prop: "index",
    width: 80,
    align: "center",
    render: (row, column, cellValue, index) => index + 1,
  },
  {
    label: "发布时间",
    prop: "disclosure_date",
    minWidth: 120,
    align: "center",
  },
  {
    label: "情报名称",
    prop: "vuln_name",
    minWidth: 300,
    render: (row, column, cellValue, index) => {
      return h(
        "div",
        { style: { display: "flex", alignItems: "center", gap: "8px" } },
        [
          h(
            "span",
            {
              style: {
                color: row.hasRead ? "#67c23a" : "#f56c6c",
                fontSize: "12px",
                fontWeight: "bold",
              },
            }
            // row.hasRead ? "[已读]" : "[未读]"
          ),
          h("span", row.name),
        ]
      );
    },
  },
  // {
  //   label: "情报类型",
  //   prop: "type",
  //   minWidth: 100,
  //   align: "center",
  //   render: (row, column, cellValue, index) => {
  //     const levelConfig = vulnLevelMap[
  //       row.type as keyof typeof vulnLevelMap
  //     ] || { text: row.type, type: "info" };
  //     return h(
  //       "el-tag",
  //       {
  //         type: levelConfig.type,
  //         size: "small",
  //       },
  //       levelConfig.text
  //     );
  //   },
  // },
  {
    label: "情报来源",
    prop: "origin",
    minWidth: 120,
    align: "center",
    render: (row, column, cellValue, index) => {
      return h(
        "div",
        {
          style: {
            color: "#909399",
            fontWeight: "normal",
          },
        },
        "topsec"
      );
    },
  },
  {
    label: "受影响资产数",
    prop: "yingxiang",
    minWidth: 120,
    align: "center",
    render: (row, column, cellValue, index) => {
      return h(
        "div",
        {
          style: {
            color: row.yingxiang > 0 ? "#f56c6c" : "#909399",
            fontWeight: row.yingxiang > 0 ? "bold" : "normal",
          },
        },
        row.yingxiang
      );
    },
  },
  {
    label: "操作",
    prop: "operation",
    width: 120,
    fixed: "right",
    align: "center",
    slot: "operation",
    // render: (row, column, cellValue, index) => {
    //   return h(
    //     "el-button",
    //     {
    //       type: "primary",
    //       size: "small",
    //       onClick: () => goVulnerDetail(row),
    //     },
    //     "查看详情"
    //   );
    // },
  },
];

// 查看详情
const goVulnerDetail = async (row: any) => {
  try {
    // 如果有 avd_id，可以获取详细信息
    if (row.avd_id) {
      // 这里可以调用获取详情的 API
      console.log("查看漏洞详情:", row.avd_id);
    }
    router.push("/intelligence/vulnerdetail");
  } catch (error) {
    console.error("获取漏洞详情失败:", error);
  }
};

// 导出默认的表格列配置
export const tableColumns = createTableColumns();
